﻿package com.farm.quartz.web.action;

import com.farm.quartz.domain.FarmQzScheduler;
import com.farm.quartz.domain.FarmQzTask;
import com.farm.quartz.server.FarmQzSchedulerManagerInter;

import com.farm.web.easyui.EasyUiUtils;
import java.util.Map;
import org.apache.log4j.Logger;
import org.quartz.Job;

import com.farm.core.page.CommitType;
import com.farm.core.page.PageSet;
import com.farm.core.page.PageType;
import com.farm.core.sql.query.DataQuery;
import com.farm.core.sql.result.DataResult;
import com.farm.web.WebSupport;
import com.farm.web.spring.BeanFactory;

/**
 * 计划任务管理
 * 
 * @author autoCode
 * 
 */
public class ActionFarmQzSchedulerQuery extends WebSupport {
	private Map<String, Object> jsonResult;// 结果集合
	private DataQuery query;// 条件查询
	private FarmQzScheduler entity;// 实体封装
	private PageSet pageset;// 请求状态
	private String ids;// 主键集合
	private String taskStr;
	private String triggerStr;

	/**
	 * 查询结果集合
	 * 
	 * @return
	 */
	public String queryall() {
		try {
			query = EasyUiUtils.formatGridQuery(getRequest(), query);
			DataResult result = aloneIMP.createSchedulerSimpleQuery(query)
					.search().runDictionary("1:自动,0:手动", "AUTOIS");
			for (Map<String, Object> node : result.getResultList()) {
				try {
					node.put("RUN", aloneIMP.isRunningFindScheduler(node.get(
							"ID").toString()));
					node.put("RUNTYPE", node.get("RUN"));
				} catch (Exception e) {
					log.error(e);
				}
			}
			result.runDictionary("true:是,false:否", "RUN");
			jsonResult = EasyUiUtils.formatGridData(result);
		} catch (Exception e) {
			log.error(e);
			throw new RuntimeException(e);
		}
		return SUCCESS;
	}

	public String start() {
		try {
			for (String id : parseIds(ids)) {
				aloneIMP.startTask(id);
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	public String stop() {
		try {
			for (String id : parseIds(ids)) {
				aloneIMP.stopTask(id);
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	public String runOnec() {
		try {
			int successNum = 0;
			for (@SuppressWarnings("unused") String id : parseIds(ids)) {
				try {
					FarmQzTask task = aloneIMP.getTaskBySchedulerId(ids);
					Job job = (Job) Class.forName(task.getJobclass())
							.newInstance();
					job.execute(null);
					successNum++;
				} catch (Exception e) {
					log.error(e.getMessage());
				}
			}
			pageset = new PageSet(PageType.OTHER, CommitType.TRUE);
			pageset.setMessage(parseIds(ids).size()
					+ "条任务被执行,其中" + successNum + "条成功");
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交修改数据
	 * 
	 * @return
	 */
	public String editSubmit() {
		try {
			entity = aloneIMP.editSchedulerEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.UPDATE, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.UPDATE,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 提交新增数据
	 * 
	 * @return
	 */
	public String addSubmit() {
		try {
			entity = aloneIMP.insertSchedulerEntity(entity, getCurrentUser());
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.ADD,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 删除数据
	 * 
	 * @return
	 */
	public String delSubmit() {
		try {
			for (String id : parseIds(ids)) {
				aloneIMP.deleteSchedulerEntity(id, getCurrentUser());
			}
			pageset = new PageSet(PageType.ADD, CommitType.TRUE);
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.DEL,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	/**
	 * 跳转
	 * 
	 * @return
	 */
	public String forSend() {
		return SUCCESS;
	}

	/**
	 * 显示详细信息（修改或浏览时）
	 * 
	 * @return
	 */
	public String view() {
		try {
			switch (pageset.getPageType()) {
			case (1): {// 新增
				return SUCCESS;
			}
			case (0): {// 展示
				entity = aloneIMP.getSchedulerEntity(ids);
				taskStr = aloneIMP.getTaskEntity(entity.getTaskid()).getName();
				triggerStr = aloneIMP.getTriggerEntity(entity.getTriggerid())
						.getName();
				return SUCCESS;
			}
			case (2): {// 修改
				entity = aloneIMP.getSchedulerEntity(ids);
				taskStr = aloneIMP.getTaskEntity(entity.getTaskid()).getName();
				triggerStr = aloneIMP.getTriggerEntity(entity.getTriggerid())
						.getName();
				return SUCCESS;
			}
			default:
				break;
			}
		} catch (Exception e) {
			pageset = PageSet.initPageSet(pageset, PageType.OTHER,
					CommitType.FALSE, e);
		}
		return SUCCESS;
	}

	private final static FarmQzSchedulerManagerInter aloneIMP = (FarmQzSchedulerManagerInter) BeanFactory
			.getBean("farm_qz_schedulerProxyId");

	// ----------------------------------------------------------------------------------
	public DataQuery getQuery() {
		return query;
	}

	public void setQuery(DataQuery query) {
		this.query = query;
	}

	public FarmQzScheduler getEntity() {
		return entity;
	}

	public void setEntity(FarmQzScheduler entity) {
		this.entity = entity;
	}

	public PageSet getPageset() {
		return pageset;
	}

	public void setPageset(PageSet pageset) {
		this.pageset = pageset;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Map<String, Object> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, Object> jsonResult) {
		this.jsonResult = jsonResult;
	}

	public String getTaskStr() {
		return taskStr;
	}

	public void setTaskStr(String taskStr) {
		this.taskStr = taskStr;
	}

	public String getTriggerStr() {
		return triggerStr;
	}

	public void setTriggerStr(String triggerStr) {
		this.triggerStr = triggerStr;
	}

	private static final Logger log = Logger
			.getLogger(ActionFarmQzSchedulerQuery.class);
	private static final long serialVersionUID = 1L;
	/**
	 * 加载树节点 public String loadTreeNode() { treeNodes =
	 * EasyUiTreeNode.formatAjaxTree(EasyUiTreeNode .queryTreeNodeOne(id,
	 * "SORT", "alone_menu", "ID", "PARENTID", "NAME").getResultList(),
	 * EasyUiTreeNode .queryTreeNodeTow(id, "SORT", "alone_menu", "ID",
	 * "PARENTID", "NAME").getResultList(), "PARENTID", "ID", "NAME"); return
	 * SUCCESS; }
	 * 
	 * @return
	 */
}
